﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.TradeLanguage.Data;

namespace AAA.TradeLanguage
{
    public class PositionManager
    {
        private Dictionary<string, List<PositionInfo>> _dicCurrentPosition;
        public PositionManager()
        {
            Reset();
        }

        public void Reset()
        {
            _dicCurrentPosition = new Dictionary<string, List<PositionInfo>>();
        }

        public void SetPosition(string strSignal, List<PositionInfo> lstPosition)
        {
            if (_dicCurrentPosition.ContainsKey(strSignal))
                _dicCurrentPosition[strSignal] = lstPosition;
            else
                _dicCurrentPosition.Add(strSignal, lstPosition);
        }

        public void AddPosition(string strSignal, PositionInfo position)
        {
            List<PositionInfo> lstPosition = new List<PositionInfo>();
            lstPosition.Add(position);
            AddPosition(strSignal, lstPosition);
        }

        public void AddPosition(string strSignal, List<PositionInfo> lstPosition)
        {
            if (_dicCurrentPosition.ContainsKey(strSignal))
            {
                SetPosition(strSignal, lstPosition);
                return;
            }

            foreach (PositionInfo position in lstPosition)
                _dicCurrentPosition[strSignal].Add(position);
        }

        public List<PositionInfo> GetPosition(string strSignal)
        {
            return _dicCurrentPosition.ContainsKey(strSignal)
                    ? _dicCurrentPosition[strSignal]
                    : new List<PositionInfo>();               
        }

        public string[] GetAllSignals()
        {
            string[] strSignals = new string[_dicCurrentPosition.Count];
            int iIndex = 0;

            foreach (string strSignalName in _dicCurrentPosition.Keys)
            {
                strSignals[iIndex++] = strSignalName;
            }
            return strSignals;
        }

        public List<PositionInfo>[] GetAllPosition()
        {
            List<PositionInfo>[] lstPositions = new List<PositionInfo>[_dicCurrentPosition.Count];

            int iIndex = 0;

            foreach (List<PositionInfo> lstPosition in _dicCurrentPosition.Values)
            {
                lstPositions[iIndex++] = lstPosition;
            }

            return lstPositions;
        }

        
/*
        public int GetPositionBySymbol(string strSymbolId)
        {
            List<PositionInfo>[] lstPositions = GetAllPosition();

            int iPosition = 0;

            for (int i = 0; i < lstPositions.Length; i++)
                for(int j = 0; j < lstPositions[i].Count; j++)
                    iPosition += lstPositions[i][j].Position;

            return iPosition;
        }
 */ 
    }
}
